package com.tm.szb.controller;


import com.tm.szb.commons.R;
import org.apache.commons.lang.mutable.MutableObject;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@RestController
@RequestMapping("common")
public class CommonController {

    @PostMapping("upload")
    public R upload(@RequestBody MultipartFile file){
        try {
            String fileName = file.getOriginalFilename();
            String substring = fileName.substring(fileName.lastIndexOf("."));
            String newFilename = UUID.randomUUID().toString().replace("-", "") + substring;
            String path = ResourceUtils.getURL("classpath:").getPath()+"static/";
            File file1 = new File(path);
            if (file1.exists()) {
                file1.mkdir();
            }
            File file2 = new File(path,newFilename);
            file.transferTo(file2);
            return R.success(newFilename);
        }catch (IOException e){
            e.printStackTrace();
            return R.error();
        }

    }
}
